<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="订单名称" prop="postName">
           <el-input
              v-model="queryParams.postName"
              placeholder="请输入订单名称"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
           />
        </el-form-item> -->
        <!-- <el-form-item label="状态" prop="status">
           <el-select v-model="queryParams.status" placeholder="订单状态" clearable style="width: 200px">
              <el-option
                 v-for="dict in stateArray"
                 :key="dict.value"
                 :label="dict.label"
                 :value="dict.value"
              />
           </el-select>
        </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
           <el-button
              type="primary"
              plain
              icon="Plus"
              @click="handleAdd"
              v-hasPermi="['goods:index:add']"
           >新增</el-button>
        </el-col> -->
      <!-- <el-col :span="1.5">
           <el-button
              type="success"
              plain
              icon="Edit"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:post:edit']"
           >修改</el-button>
        </el-col>
        <el-col :span="1.5">
           <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:post:remove']"
           >删除</el-button>
        </el-col> -->
      <!-- <el-col :span="1.5">
           <el-button
              type="warning"
              plain
              icon="Download"
              @click="handleExport"
              v-hasPermi="['goods:index:export']"
           >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>
    <!-- <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
      <el-tab-pane label="User" name="first">User</el-tab-pane>
      <el-tab-pane label="Config" name="second">Config</el-tab-pane>
      <el-tab-pane label="Role" name="third">Role</el-tab-pane>
      <el-tab-pane label="Task" name="fourth">Task</el-tab-pane>
    </el-tabs> -->
    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        label="订单信息"
        align="left"

        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <div class="order_items">
            <!-- <div>
              <el-image :src="scope.row.skuList[0].pic_url" style="width: 42px;height: 42px;"  >  </el-image>
            </div> -->
            <div class="order_item_right">
              <span class="word_over"
                >{{ scope.row.skuList[0].goods_name }}({{
                  scope.row.skuList[0].goods_sku_no
                }}){{ scope.row.skuList.length > 1 ? "..." : "" }}</span
              >
              <span>共{{ scope.row.skuList.length }}件</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="订单号"
        width="180"
        prop="orderNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="下单人"
        width="150"
        align="center"
        prop="memName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="订单金额(元)"
        align="center"
        prop=""
        width="180"
      >
        <template #default="scope">
          <span v-if="scope.row.goodsType == 1">{{ scope.row.updatePrice }}</span>
          <span v-if="scope.row.goodsType == 4">
            <span v-if="scope.row.updatePrice !='0.00'">{{ scope.row.updatePrice }}+</span>
            {{scope.row.pointsNum}}饭券
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="订单状态"
        width="80"
        align="center"
        prop="orderStatusName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="商品类型" align="center" prop="goodsType" width="100" :show-overflow-tooltip="true" >
        <template #default="scope">
            <dict-tag :options="goods_type" :value="scope.row.goodsType" />
        </template>
      </el-table-column>
      <el-table-column
        label="下单时间"
        align="center"
        prop="createTime"
        width="160"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="支付时间"
        align="center"
        prop="createTime"
        width="160"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) || "-" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="支付方式"
        align="center"
        prop=""
        width="100"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{payTypeList[scope.row.payType] }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        width="200"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Promotion"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.orderStatus == 4"
            v-hasPermi="['order:index:send']"
            >发货</el-button
          >
          <el-button
            link
            type="primary"
            icon="View"
            v-hasPermi="['order:index:view']"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <!-- <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['goods:index:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 发货订单对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <div class="form_box">
        <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
          <!-- <el-row>
            <el-col :span="18"> 订单编号:{{ detailObject.orderNo }} </el-col>
          </el-row> -->
          <!-- <el-row>
            <el-col :span="18"> 订单编号:{{ detailObject.orderNo }} </el-col>
          </el-row> -->
          <el-row>
            <el-col :span="18">
              <el-form-item label="物流单号" prop="expressId">
                <el-input
                  v-model="form.expressId"
                  placeholder="请输入物流单号"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="18">
              <el-form-item label="物流公司" prop="expressName">
                <el-input
                  v-model="form.expressName"
                  placeholder="请输入物流公司"
                />
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 订单详情对话框 -->

    <el-dialog
      :title="detailTitle"
      v-model="detailOpen"
      width="810px"
      append-to-body
      custom-class="views111"
    >
      <div class="detail_content">
        <div class="detail_title">
          <div class="price" v-if="detailObject.goodsType == 1">订单金额：
            ¥ {{ detailObject.updatePrice }}
          </div>
          <div class="price" v-if="detailObject.goodsType == 4">订单金额：
            <span v-if="detailObject.updatePrice !='0.00'">¥ {{ detailObject.updatePrice }}+</span>
            {{ detailObject.pointsNum }}饭券
          </div>
        </div>
        <h3>订单信息</h3>
        <el-descriptions
          class="margin-top"
          :column="2"
          :size="size"


          border
        >
          <el-descriptions-item label="订单号">{{
            detailObject.orderNo
          }}</el-descriptions-item>
          <el-descriptions-item label="订单状态">{{
            detailObject.orderStatusName
          }}</el-descriptions-item>
          <el-descriptions-item label="付款状态">{{
            detailObject.payStatusName
          }}</el-descriptions-item>
           <el-descriptions-item label="下单时间">{{
            detailObject.createTime || "-"
          }}</el-descriptions-item>
           <el-descriptions-item label="付款时间">{{
            detailObject.payTime || "-"
          }}</el-descriptions-item>
          <el-descriptions-item label="商品类型">
            {{ detailObject.goodsType ==1?'普通商品':'饭券商品'}}
            <!-- <dict-tag :options="goods_type" :value="detailObject.goodsType" /> -->
          </el-descriptions-item>



           <el-descriptions-item label="支付方式">
            {{payTypeList[detailObject.payType] }}
           </el-descriptions-item>




        </el-descriptions>

        <el-descriptions
          title=""
          direction="vertical"
          :column="1"


          border
        >
        <el-descriptions-item label="商品信息">
            <div class="detail_box">
              <div
                v-for="(itemx, indexx) in detailObject.orderSkuList"
                :key="indexx"
                class="detail_item"
              >
                <div style="color: #606266">{{ parseInt(indexx + 1) }}、</div>
                <el-image
                  :src="itemx.pic_url"
                  style="width: 60px; height: 60px"
                  :preview-src-list="[itemx.pic_url]"
                ></el-image>
                <div class="detail_middle">
                  <div class="detail_middles">
                    <div class="name">{{ itemx.goods_name }}</div>
                    <div class="no">{{ itemx.goods_sku_no }}</div>
                    <div class="name" v-if="detailObject.goodsType == 1">¥{{ itemx.goods_price }}</div>
                    <div class="name" v-if="detailObject.goodsType == 4">
                      <span v-if="detailObject.updatePrice !='0.00'">¥{{ detailObject.updatePrice }}+</span>{{ detailObject.pointsNum }}饭券
                    </div>
                  </div>

                  <div class="detail_right">数量：x {{ itemx.total_num }}</div>
                </div>
              </div>
            </div>
          </el-descriptions-item>
        </el-descriptions>
        <h3>收货信息</h3>
        <el-descriptions
          class="margin-top"
          :column="2"
          :size="size"
          border
        >

            <el-descriptions-item label="收货人员">
              {{  detailObject.addressInfo ?detailObject.addressInfo.name:'-' }}
            </el-descriptions-item>
            <el-descriptions-item label="收货号码">
              {{ detailObject.addressInfo ?detailObject.addressInfo.phone:'-' }}
            </el-descriptions-item>
            <el-descriptions-item label="收货地址" v-if="detailObject.addressInfo">
              {{ detailObject.addressInfo.province_name +detailObject.addressInfo.city_name+detailObject.addressInfo.region_name+detailObject.addressInfo.detail}}
            </el-descriptions-item>



          </el-descriptions>
          <h3 v-if="detailObject.orderExpressId!=0">物流信息</h3>
        <el-descriptions
          class="margin-top"
          :column="2"
          :size="size"
          border
          v-if="detailObject.orderExpressId!=0"
        >
          <el-descriptions-item label="物流公司">
            {{  detailObject.orderExpressName ||'-' }}
          </el-descriptions-item>
          <el-descriptions-item label="物流单号">
            {{  detailObject.orderExpressId!=0?detailObject.orderExpressId:'-' }}
          </el-descriptions-item>
          <el-descriptions-item label="发货时间">
            {{  detailObject.orderExpressTime ||'-' }}
          </el-descriptions-item>
          <el-descriptions-item label="收货时间">
            {{  detailObject.orderExpressReTime ||'-' }}
          </el-descriptions-item>
          <el-descriptions-item label="物流状态">
            {{  detailObject.orderExpressStatusName ||'-' }}
          </el-descriptions-item>
          </el-descriptions>
      </div>
      <!-- <template #footer v-if="detailObject.orderStatus == 4">
        <div class="dialog-footer">
          <el-button type="primary" @click="handleUpdate">发 货</el-button>

        </div>
      </template> -->
    </el-dialog>
  </div>
</template>

<script setup name="orderManage">
import { getOrderList, sendOut, getOrderDetail } from "@/api/order/list";

const { proxy } = getCurrentInstance();
const { goods_type } = proxy.useDict("goods_type");
const orderList = ref([]);
const skuList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("发货");
const detailTitle = ref("订单详情");
const detailOpen = ref(false);



const data = reactive({
  form: {},
  detailObject: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: undefined,
    status: undefined,
  },
  payTypeList:{
    10:'余额支付',
    20:'微信支付',
    30:'饭券支付'
  },
  rules: {
    expressName: [
      { required: true, message: "物流公司不能为空", trigger: "blur" },
    ],
    expressId: [
      { required: true, message: "物流单号不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules, detailObject,payTypeList} = toRefs(data);

/** 查询订单列表 */
function getList() {
  loading.value = true;
  getOrderList(queryParams.value).then((response) => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    orderId: undefined,
    expressId: undefined,
    expressName: undefined,
  };
  proxy.resetForm("orderRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 查看订单详情 */
function handleView(row) {
  const orderId = row.orderId;
  getOrderDetail({ orderId: orderId }).then((response) => {
    detailObject.value = response.data;
    detailOpen.value = true;
  });
}
/** 发货按钮操作 */
function handleUpdate(row) {
  reset();
  detailObject.value = row;
  form.value.orderId = row.orderId;
  open.value = true;
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate((valid) => {
    if (valid) {
      sendOut(form.value).then((response) => {
        proxy.$modal.msgSuccess("发货成功");
        open.value = false;
        getList();
      });
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const goodsId = row.goodsId;
  proxy.$modal
    .confirm('是否确认删除名称为"' + row.goodsName + '"的数据项？')
    .then(function () {
      return deleteGoods(goodsId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/post/export",
    {
      ...queryParams.value,
    },
    `post_${new Date().getTime()}.xlsx`
  );
}
/** 添加规格 */
function handleTableAdd() {
  skuList.value.push({
    goods_sku_no: "",
    pic_url: "",
    goods_price: 0,
    stock_num: 0,
  });
}
getList();
</script>
<style>
  .views111{
    .el-dialog__body{
      padding-top: 0;
    }
  }
</style>
<style lang="scss" scoped>
.order_items {
  display: flex;
  align-items: center;
  .order_item_right {
    display: flex;
    flex-direction: column;
  }
  .word_over {
    width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
.detail_content {
  height: 70vh;
  overflow: auto;
}
.detail_title {
  display: flex;
  justify-content: space-between;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 30px;
}

.order_item {
  display: flex;
  flex-direction: column;
}
.order_items {
  display: flex;
  align-items: center;
  .order_item_right {
    display: flex;
    flex-direction: column;
  }
}
.detail_box {
  display: flex;
  flex-direction: column;
}

.detail_title {
  display: flex;
  justify-content: space-between;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 30px;
}
.detail_item:last-child{
  border-bottom: none;
}
.detail_item {
  width: 100%;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #dddd;
  padding: 5px;
  box-sizing: border-box;
  .detail_middle {
    margin-left: 10px;
    display: flex;
    align-items: center;

    justify-content: space-between;
  }
  .detail_middles {
    width: 450px;
    display: flex;
    flex-direction: column;

    justify-content: space-between;
    .name {
      color: #606266;
      font-size: 13px;
    }
    .no {
      font-size: 12px;
      color: #aaa;
    }
  }
  .detail_right {
    width: 100px;
    margin-left: 50px;
    display: flex;
    font-size: 12px;
  }
}
</style>
